package cn.ac.jaas.ljx.wordsegmentation.interfaces.impl;

import java.util.ArrayList;
import java.util.List;

import org.apdplat.word.tagging.PartOfSpeechTagging;
import cn.ac.jaas.ljx.wordsegmentation.domain.Document;
import cn.ac.jaas.ljx.wordsegmentation.domain.Word;
import cn.ac.jaas.ljx.wordsegmentation.interfaces.Segmenter;
import cn.ac.jaas.ljx.wordsegmentation.util.WordCountUtil;
import org.nlpcn.commons.lang.util.StringUtil;

public class WordSegmenter implements Segmenter {

	@Override
	public List<Word> segment(Document document) {
		String text = document.getTitle() + document.getDigest();
		if(StringUtil.isBlank(text)) {
			return new ArrayList<Word>();
		}
		List<org.apdplat.word.segmentation.Word> words = org.apdplat.word.WordSegmenter.segWithStopWords(text);
		PartOfSpeechTagging.process(words);
		List<Word> wordsNoCount = new ArrayList<>();
		List<Word> wordsCounted = new ArrayList<>();
		for(org.apdplat.word.segmentation.Word  word : words) {
			Word wordNoCount = new Word();
			wordNoCount.setValue(word.getText());
			wordNoCount.setNature(word.getPartOfSpeech().getPos());
			//System.out.println(wordNoCount.toString());
			wordsNoCount.add(wordNoCount);
			
		}
		WordCountUtil.wordCount(wordsNoCount, wordsCounted);
		return wordsCounted;
	}

}
